package com.efounder.news.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.mobilenews.R;
import com.efounder.news.bean.CommentReplyBean;
import com.efounder.news.http.Api;
import com.efounder.news.irecyclerview.IViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论详情页的Adapter
 * Created by slp on 2017/11/24.
 */

public class DetailCommentAdapter extends RecyclerView.Adapter {

    private final String TAG = DetailCommentAdapter.class.getSimpleName();

    private static final int NORMAL_COMMENT = 0;//普通评论
    private static final int REPALY_COMMENT = 1;//回复的评论

    private Context mContext;

    private List<CommentReplyBean> mCommentList;

    private OnItemClickListener mOnItemClickListener;
    //点赞
    private OnGoodClickListener mOnGoodClickListener;
    //取消赞
    private OnBadClickListener mOnBadClickListener;

    public DetailCommentAdapter(Context context, List<CommentReplyBean> commentList) {
        mContext = context;
        mCommentList = commentList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == REPALY_COMMENT) {
            return null;
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_detail_comment, parent, false);
            return new NormalViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        holder.itemView.setVisibility(View.VISIBLE);
        CommentReplyBean commentListBean = mCommentList.get(position);
        ArrayList<CommentReplyBean.ReplyReplyBean> replyReplyList = commentListBean.getReplyList();
        final NormalViewHolder normalViewHolder = (NormalViewHolder) holder;
//        String newid = commentListBean.getNewsId();
        String commentDate = commentListBean.getDate();

        String userName = commentListBean.getUserName();
        String userId = commentListBean.getUserId();

        if (commentListBean.getDiggCount().equals("0")) {
            normalViewHolder.favourCount.setText(R.string.detail_zan);
        } else {
            normalViewHolder.favourCount.setText(commentListBean.getDiggCount());
        }
        String userDigg = commentListBean.getUserDigg();
        if (userDigg.equals("1")) {
            normalViewHolder.likeImg.setImageResource(R.drawable.good_checked);
        } else {
            normalViewHolder.likeImg.setImageResource(R.drawable.good);
        }

        normalViewHolder.createTime.setText(commentDate);
        if (replyReplyList.size() != 0) {
            SpannableStringBuilder comment = new SpannableStringBuilder(commentListBean.getText() + "//");
            CommentReplyBean.ReplyReplyBean replyReplyBean = replyReplyList.get(0);

            String replyName = "@" + replyReplyBean.getUserName();
            SpannableString replyNamestr = new SpannableString(replyName);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#406599"));
            replyNamestr.setSpan(colorSpan, 0, replyNamestr.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);


            SpannableString replyComment = new SpannableString(":" + replyReplyBean.getText());

            ForegroundColorSpan color = new ForegroundColorSpan(mContext.getResources().getColor(R.color.black_text_color));
            replyComment.setSpan(color, 0, replyComment.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

            comment.append(replyNamestr);
            comment.append(replyComment);

            normalViewHolder.text.setText(comment);

        } else {
            String comment = commentListBean.getText();
            normalViewHolder.text.setText(comment);
        }
        normalViewHolder.userName.setText(userName);
        normalViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = normalViewHolder.getIAdapterPosition();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(normalViewHolder.itemView, pos);
                }
            }
        });

        normalViewHolder.likeImg.setOnClickListener(new View.OnClickListener() {
            CommentReplyBean mCommentListBean = mCommentList.get(position);
            String userDigg = mCommentListBean.getUserDigg();
            String diggCount = mCommentListBean.getDiggCount();

            @Override
            public void onClick(View v) {

                if (userDigg.equals("0")) {
                    int dCount = Integer.parseInt(diggCount);
                    dCount += 1;
                    diggCount = String.valueOf(dCount);
                    mCommentListBean.setUserDigg("1");
                    mCommentListBean.setDiggCount(diggCount);
                    if (mOnGoodClickListener != null) {
                        mOnGoodClickListener.onGoodClick(position);
                    }
                } else {
                    mCommentListBean.setUserDigg("0");
                    int dCount = Integer.parseInt(diggCount);
                    dCount -= 1;
                    diggCount = String.valueOf(dCount);
                    mCommentListBean.setDiggCount(diggCount);

                    if (mOnBadClickListener != null) {
                        mOnBadClickListener.onBadClick(position);
                    }
                }
            }
        });

        setNetPicture(Api.getAvator + userId, normalViewHolder.userImg);
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = NORMAL_COMMENT;
        return viewType;
    }

    @Override
    public int getItemCount() {
        return mCommentList.size();
    }

    private void setNetPicture(String url, final ImageView img) {
        Glide
                .with(mContext)
                .load(url)
                .transition(new DrawableTransitionOptions().crossFade())
                .apply(new RequestOptions().placeholder(R.mipmap.default_userhead)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .transform(new CircleCrop()))
                .into(img);
    }

    class NormalViewHolder extends IViewHolder {

        public ImageView userImg;
        public TextView userName;
        public ImageView likeImg;
        public TextView favourCount;
        public TextView text;
        public TextView replyNum;
        public TextView createTime;

        private NormalViewHolder(View itemView) {
            super(itemView);
            userName = (TextView) itemView.findViewById(R.id.tv_name);
            userImg = (ImageView) itemView.findViewById(R.id.iv_avatar);
            likeImg = (ImageView) itemView.findViewById(R.id.iv_like);
            favourCount = (TextView) itemView.findViewById(R.id.tv_like_count);
            text = (TextView) itemView.findViewById(R.id.tv_comment_item);
            createTime = (TextView) itemView.findViewById(R.id.tv_comment_time);
            replyNum = (TextView) itemView.findViewById(R.id.tv_reply_num);
        }
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    /**
     * 设置点赞监听
     *
     * @param listener
     */
    public void setOnGoodClickListener(OnGoodClickListener listener) {
        this.mOnGoodClickListener = listener;

    }

    /**
     * 设置取消赞监听
     *
     * @param listener
     */
    public void setOnBadClickListener(OnBadClickListener listener) {
        this.mOnBadClickListener = listener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(View v, int position);
    }

    public interface OnGoodClickListener<T> {
        void onGoodClick(int position);
    }

    public interface OnBadClickListener<T> {
        void onBadClick(int position);
    }
}
